<?php
/**
 * Created by PhpStorm.
 * User: wx:wenyi2048
 * Date: 2022/3/19 0019
 * Time: 16:45
 */

namespace app\common\service;


use think\Db;
use think\Exception;

class GoodsService extends Service
{
    protected $tableName = 'sms_mk_goods';

    protected $userAdminId;//店铺管理员id

    protected $store = [];


    /**
     * @param $view
     * @param $params
     * @param int $isdelete  返回全部商品分页
     */
    public function searchPage(&$view,$params,$isdelete = 0){

        $this->db->where('isdelete','=',$isdelete)
            ->order('id','desc')
            ->field('*');
        //添加店铺查询
        //$storeId = getAdminStoreId(@$params['store_id']);
        $storeId = $this->checkStoreSelect(@$params['store_id']);
        if($storeId) $this->db->where('sms_stores_id','=',$storeId);
        if(@$params['name']) $this->db->where('name','like','%'.$params['name'].'%');
        if(@$params['sms_store_types_id']) $this->db->where('sms_store_types_id','=',$params['sms_store_types_id']);
        $view->assign('sms_stores_id',$storeId);
        $this->pageList($view,$params);
    }

    public function topList(&$view,$params,$isdelete = 0){
        $this->db->alias('g')
            ->join('sms_mk_goods_tops t','g.id = t.goods_id')
            ->where('g.isdelete','=',$isdelete)
            ->order('g.id','desc')
            ->field('g.*');
        if(@$params['name']) $this->db->where('g.name','like','%'.$params['name'].'%');

        $this->pageList($view,$params);
    }


    public function datalist()
    {
        $lng = @$this->param['lng'];
        $lat = @$this->param['lat'];
        $this->setDb('goods_view');
//        if($lng || $lat)
//            $this->db->field("*,(st_distance(point(lng,lat),point($lng,$lat))*111195/1000 ) as distance")
//                ->order('distance','asc');
        if($lng && $lat){
            $this->db->field("*,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(($lat * PI()/ 180-lat * PI()/ 180)/ 2),2)+ COS( $lat * PI()/ 180 )* COS( lat * PI()/ 180 )* POW(SIN(($lng * PI()/ 180-lng * PI()/ 180)/ 2),2)))* 1000) AS distance")
                ->order('distance','asc');
        }
        $store_id = @$this->param['store_id'];
        $search_name = @$this->param['search_name'];
        $types_first_id = @$this->param['types_first_id'];
        $types_second_id = @$this->param['types_second_id'];
        $types_third_id = @$this->param['types_third_id'];
        $store_types_id = @$this->param['store_types_id'];

        if($types_first_id)
            $this->db = $this->db->where('types_first_id','=',$types_first_id);
        elseif($types_second_id){
            $this->db = $this->db->where('types_second_id','=',$types_second_id);
        }elseif($types_third_id){
            $this->db = $this->db->where('types_third_id','=',$types_third_id);
        }
        if($store_id)
            $this->db = $this->db->where('store_id','=',$store_id);
        if($store_types_id)
            $this->db = $this->db->where('store_types_id','=',$store_types_id);
        if($search_name){
            $this->db = $this->db->whereOr('types_first_text','like','%'.$search_name.'%');
            $this->db = $this->db->whereOr('store_types_text','like','%'.$search_name.'%');
            $this->db = $this->db->whereOr('types_second_text','like','%'.$search_name.'%');
            $this->db = $this->db->whereOr('types_third_text','like','%'.$search_name.'%');
            $this->db = $this->db->whereOr('name','like','%'.$search_name.'%');
            $this->db = $this->db->whereOr('store_name','like','%'.$search_name.'%');
            $this->db = $this->db->whereOr('store_tag','like','%'.$search_name.'%');
        }

        $result =  parent::datalist(); // TODO: Change the autogenerated stub
        return $result;
    }

    public function dataListTop(){
        $lng = @$this->param['lng'];
        $lat = @$this->param['lat'];
        $this->setDb('goods_view');
        if($lng && $lat){
            $this->db->field("*,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(($lat * PI()/ 180-g.lat * PI()/ 180)/ 2),2)+ COS( $lat * PI()/ 180 )* COS( g.lat * PI()/ 180 )* POW(SIN(($lng * PI()/ 180-g.lng * PI()/ 180)/ 2),2)))* 1000) AS distance")
                ->order('distance','asc');
        }
        $this->db->alias('g')
            ->join('sms_mk_goods_tops t','t.goods_id = g.id','right')
            ->field('g.*');
        return  parent::datalist();
    }

    public function getGoods($id){
        $this->setDb('goods_view');
        $this->db->where('id','=',$id);
        return $this->db->select();
    }

    public function getGoodsStar(){
        $this->setDb('goods_view');
        $stars = Db::table('sms_mk_goods_star')
            ->where('sms_users_id','=',$this->param['sms_users_id'])
            ->column('sms_goods_id');
        $result = array();
        if($stars){
            $this->db->whereIn('id',$stars);
            $result = parent::datalist();
                //->select();
        }
        return $result;
    }

    public function getGoodsStarId(){
        return Db::table('sms_mk_goods_star')
            ->where('sms_users_id','=',$this->param['sms_users_id'])
            ->where('sms_goods_id','=',$this->param['goods_id'])
            ->find();
    }

    public function setGoodsStar(){
        if(!is_array($this->param['goods_id'])) $this->param['goods_id'] = [$this->param['goods_id']];
        $stars = Db::table('sms_mk_goods_star')
            ->where('sms_users_id','=',$this->param['sms_users_id'])
            ->where('sms_goods_id','=',$this->param['goods_id'])
            ->find();
        if($stars){
            return Db::table('sms_mk_goods_star')
                ->where('sms_users_id','=',$this->param['sms_users_id'])
                ->whereIn('sms_goods_id',$this->param['goods_id'])
                ->delete();
        }else{
            $data = [
                'sms_users_id'=>$this->param['sms_users_id'],
                'sms_goods_id'=>$this->param['goods_id']
            ];
            return Db::table('sms_mk_goods_star')->insertGetId($data);
        }
    }


    public function topAdd($goodsIds){
        if(!$goodsIds) throw new Exception('请选择商品');
        if(!is_array($goodsIds)) $goodsIds = array($goodsIds);
        if($goodsIds)
            Db::table('sms_mk_goods_tops')->whereIn('goods_id',$goodsIds)->delete();
        $ids = [];
        foreach ($goodsIds as $id){
            $ids[]['goods_id'] = $id;
        }
        if(!$ids)  throw new Exception('没有找到商品');
        return Db::table('sms_mk_goods_tops')->insertAll($ids);
    }

    public function topDelete($goodsIds){
        if(is_string($goodsIds)) $goodsIds = explode(',',$goodsIds);
        if(!is_array($goodsIds)) return 0;
        return Db::table('sms_mk_goods_tops')->whereIn('goods_id',$goodsIds)->delete();
    }

    public function getStoreGoods(){
        $this->setDb('sms_mk_goods');
        $this->db->where('sms_store_types_id','=',$this->param['store_types_id'])
            ->where('sms_stores_id','=',$this->param['store_id'])
            ->order('id','desc');
        return parent::datalist();
    }
}